package com.group4.admin.beans;

import com.group4.controller.CourseFacade;
import com.group4.controller.StudentFacade;
import com.group4.controller.TeacherFacade;
import com.group4.entity.Course;
import com.group4.entity.Student;
import com.group4.entity.Teacher;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author andake, anjonas
 */
@ManagedBean
@RequestScoped
public class AdminCoursesBackingBean {

    @EJB
    private CourseFacade courseDb;
    @EJB
    private TeacherFacade teacherDb;
    @EJB
    private StudentFacade studentDb; 
    
    private Course newCourse;
    private Long teacherId;
    private String editId;

    /** Creates a new instance of AdminCoursesBackingBean */
    public AdminCoursesBackingBean() {
        newCourse = new Course();
    }

    // adds a new course
    public void addCourse() {
        newCourse.setTeacher(teacherDb.find(teacherId));
        courseDb.create(newCourse);
        newCourse = new Course();
    }
    
    // removes a course 
    public void removeCourse(Course c) {
        for (Student s : c.getStudents()) {
            List<Course> cs = s.getsCourses();
            cs.remove(c);
            studentDb.edit(s);
        }
        courseDb.remove(c);
    }
    
    // edit a course
    public void editCourse() {
        Course c = courseDb.find(Long.valueOf(editId));
        c.setCourseName(newCourse.getCourseName());
        c.setYearNumber(newCourse.getYearNumber());
        c.setTeacher(teacherDb.find(teacherId));
        courseDb.edit(c);
        editId = null;
        newCourse = new Course();
    }
    
    // GETTERS & SETTERS 
    public List<Course> getCourses() {
        return courseDb.findAll();
    }

    public Course getNewCourse() {
        return newCourse;
    }

    public void setNewCourse(Course newCourse) {
        this.newCourse = newCourse;
    }

    public List<Teacher> getTeachers() {
        return teacherDb.findAll();
    }

    public Long getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(Long teacherId) {
        this.teacherId = teacherId;
    }

    public String getEditId() {
        return editId;
    }

    public void setEditId(String editId) {
        this.editId = editId;
    }
}
